<?php
namespace Common\Model;
use Common\Model\LogModel;
use Think\Upload;
use Think\Image;
/**
 * 文件相关表操作
 * 
 * @author xiaoyutab<xiaoyutab@qq.com>
 * @version v1.0.0
 * @copyright (c) 2017, xiaoyutab
 * @adtime 2017-07-13 20:32:43
 */
class FileModel extends AllModel {
    
    /**
     * 获取图片文件详情信息
     * @param number $fid 图片编号
     * @param string $field 要获取的字段
     * @return array 查询到的信息
     */
    public function get_info($fid = 0,$field = false){
        if($fid <= 0){
            return array();
        }
        $info = $this->where(array('id'=>$fid))->cache(true)->find();
        if($field){
            return $info[$field];
        }
        return $info;
    }

    /**
     * 上传图片操作
     * @param string $value 表单域名称，$_FILES['avatar']就填写avatar
     * @param string $path  保存文件夹名称。在IMAGE_SAVE_PATH配置项文件夹名后再建立一个文件夹
     * @param string $type  上传的附件类型，允许以下值：IMG、VIDEO、AUDIO
     * @return array 上传文件的信息
     */
    public function UploadOne($value = '', $path = 'image',$type = 'IMG') {
        if (empty($_FILES[$value])) {
            return false;
        }
        $upload = new Upload(array(
            'rootPath' => C('Upload.IMAGE_SAVE_PATH', null, './Public/Upload/'), // 设置上传的ROOT目录所在，默认在Public下的Upload目录
        ));
        $upload->maxSize = intval(C('Upload.FILE_maxSize', null, 2) * 1024 * 1024); //设置上传附件大小
        $upload->exts = C('Upload.'.$type, null, array('jpg', 'gif', 'jpeg', 'png')); //设置上传附件类型
        $upload->savePath = $path . '/' . date('Y-m-d') . '/';
        //上传时指定一个要上传的图片的名称,否则会把表中所有的图片都处理,
        $info = $upload->upload(array($value => $_FILES[$value]));
        if (empty($info)) {
            return $upload->getError();
        }
        $temp = $this->where(array(
            'md5'=>$info[$value]['md5'],
            'sha1'=>$info[$value]['sha1'],
            'size'=>$info[$value]['size']
        ))->find();
        if(empty($temp)){
            $key = explode('/', $info[$value]['type']);
            $thumb = $this->thumb_self($info[$value]['savepath'].$info[$value]['savename'],array(array(100,100),array(200,200)));
            $info[$value]['file_id'] = $this->create_info(array(
                'ext' => $info[$value]['ext'],
                'key' => $key[0],
                'md5' => $info[$value]['md5'],
                'sha1' => $info[$value]['sha1'],
                'size' => $info[$value]['size'],
                'type' => $info[$value]['type'],
                'file_path' => $info[$value]['savepath'].$info[$value]['savename'],
                'thumb_100' => $thumb[0]['status']?$thumb[0]['file_path']:'',
                'thumb_200' => $thumb[1]['status']?$thumb[1]['file_path']:'',
            ));
        } else {
            $info[$value]['file_id'] = $temp['id'];
        }
        return $info;
    }

    /**
     * 生成缩略图
     *       备注：生成缩略图所在目录为$file文件所在目录
     * @param string $file 文件所在位置，配置项IMAGE_SAVE_PATH所配置文件夹后的文件
     * @param array $date 缩略图大小数组。格式为[[100,100],[50,50]]
     * @param booler $thumbRemoveOrigin 是否删除原图，默认不删除false
     * @return array 缩略图文件数组，格式为[['file_path'=>'xxxxx','status'=>true],[...]]
     */
    public function thumb_self($file = '', $date = array(array(100, 100), array(120, 120)), $thumbRemoveOrigin = false) {
        if (empty($file)) {
            return false;
        }
        $image = new Image();
        $returns = array();
        foreach ($date as $k => $v) {
            $file_path = C('Upload.IMAGE_SAVE_PATH', null, './Public/Upload/') . dirname($file) . '/thumb_' . $v[0] . 'x' . $v[1] . '_' . basename($file);
            //打开要处理的图片
            $image->open(C('Upload.IMAGE_SAVE_PATH', null, './Public/Upload/') . $file);
            $image->thumb($v[0], $v[1], Image::IMAGE_THUMB_FIXED)->save($file_path);
            $returns[] = array(
                'file_path' => dirname($file) . '/thumb_' . $v[0] . 'x' . $v[1] . '_' . basename($file),
                'status' => is_file($file_path)
            );
        }
        if ($thumbRemoveOrigin) {
            unlink(C('IMAGE_SAVE_PATH', null, './Public/Upload/') . $file);
        }
        return $returns;
    }
    
    /**
     * 上传图片操作
     * @param string $value 表单域名称，$_FILES['avatar']就填写avatar
     * @param string $path  保存文件夹名称。在IMAGE_SAVE_PATH配置项文件夹名后再建立一个文件夹
     * @param string $type  上传的附件类型，允许以下值：IMG、VIDEO、AUDIO
     * @return array 上传文件的信息
     */
    public function Upload($value = '', $path = 'image',$type = 'IMG') {
        if (empty($_FILES[$value])) {
            return false;
        }
        $upload = new Upload(array(
            'rootPath' => C('Upload.IMAGE_SAVE_PATH', null, './Public/Upload/'), // 设置上传的ROOT目录所在，默认在Public下的Upload目录
        ));
        $upload->maxSize = intval(C('Upload.FILE_maxSize', null, 2) * 1024 * 1024); //设置上传附件大小
        $upload->exts = C('Upload.'.$type, null, array('jpg', 'gif', 'jpeg', 'png')); //设置上传附件类型
        $upload->savePath = $path . '/' . date('Y-m-d') . '/';
        //上传时指定一个要上传的图片的名称,否则会把表中所有的图片都处理,
        $info = $upload->upload(array($value => $_FILES[$value]));
        if (empty($info)) {
            return $upload->getError();
        }
        $temp = $this->where(array(
            'md5'=>$info[$value]['md5'],
            'sha1'=>$info[$value]['sha1'],
            'size'=>$info[$value]['size']
        ))->find();
        if(empty($temp)){
            $key = explode('/', $info[$value]['type']);
            $thumb = $this->thumb_self($info[$value]['savepath'].$info[$value]['savename'],array(array(100,100),array(200,200)));
            $info[$value]['file_id'] = $this->create_info(array(
                'ext' => $info[$value]['ext'],
                'key' => $key[0],
                'md5' => $info[$value]['md5'],
                'sha1' => $info[$value]['sha1'],
                'size' => $info[$value]['size'],
                'type' => $info[$value]['type'],
                'file_path' => $info[$value]['savepath'].$info[$value]['savename'],
                'thumb_100' => $thumb[0]['status']?$thumb[0]['file_path']:'',
                'thumb_200' => $thumb[1]['status']?$thumb[1]['file_path']:'',
            ));
            $info[$value]['file_path'] = $info[$value]['savepath'].$info[$value]['savename'];
        } else {
            $info[$value]['file_id'] = $temp['id'];
            $info[$value]['file_path'] = $temp['file_path'];
            unlink(C('Upload.IMAGE_SAVE_PATH', null, './Public/Upload/').$info[$value]['savepath'].$info[$value]['savename']);
        }
        return $info;
    }
}